import numpy as np
import wordcloud  # 词云展示库
from PIL import Image  # 图像处理库
from Request import getInterData, computeAreaTotalHotDict, getAreaData, getAnchorData, computeTopDict

# 爬虫地址字典
urlDict = {
    "amusement":
    "https://www.huya.com/cache.php?m=LiveList&do=getLiveListByPage&gameId=100022&tagAll=0&page=",
    "mobileGame":
    "https://www.huya.com/cache.php?m=LiveList&do=getLiveListByPage&gameId=100004&tagAll=0&page=",
    "onlineGame":
    "https://www.huya.com/cache.php?m=LiveList&do=getLiveListByPage&gameId=100023&tagAll=0&page=",
    "singleGame":
    "https://www.huya.com/cache.php?m=LiveList&do=getLiveListByPage&gameId=100002&tagAll=0&page="
}


# 词云生成器
def WordCloud(word_counts, path):
    # 词云构造
    mask = np.array(Image.open('./code/Python/huyaLogo1.jpg'))  # 定义词频背景
    wc = wordcloud.WordCloud(
        font_path='C:/Windows/Fonts/simhei.ttf',  # 设置字体格式（黑体）
        mask=mask,  # 设置背景图
        background_color="white",  # 设置背景颜色
        scale=5)

    wc.generate_from_frequencies(word_counts)  # 从字典生成词云
    # 输出到文件夹里
    wc.to_file(path)


# 字典合并函数
def Merge(dict1, dict2, dict3, dict4):
    res = dict1
    res.update(dict2)
    res.update(dict3)
    res.update(dict4)
    return res


# 单元测试用
if __name__ == '__main__':
    # 娱乐区爬取
    res = getInterData(urlDict["amusement"])
    area = getAreaData(res)
    amusement_wordCloud = computeAreaTotalHotDict(area)
    WordCloud(amusement_wordCloud,
              './code/wordCloudPic/amusementAreaWordCloud.png')
    print("娱乐区词云生成成功")
    anchor = getAnchorData(res, "娱乐天地")
    amusement_top = computeTopDict(anchor)
    WordCloud(amusement_top,
              './code/wordCloudPic/amusementAnchorWordCloud.png')
    print("娱乐区主播词云生成成功")
    # 手游区爬取
    res = getInterData(urlDict["mobileGame"])
    area = getAreaData(res)
    mobile_wordCloud = computeAreaTotalHotDict(area)
    WordCloud(mobile_wordCloud,
              './code/wordCloudPic/mobileGameAreaWordCloud.png')
    print("手游区词云生成成功")
    anchor = getAnchorData(res, "手游休闲")
    mobile_top = computeTopDict(anchor)
    WordCloud(mobile_top, './code/wordCloudPic/mobileGameAnchorWordCloud.png')
    print("手游区主播词云生成成功")
    # 网游区爬取
    res = getInterData(urlDict["onlineGame"])
    area = getAreaData(res)
    online_wordCloud = computeAreaTotalHotDict(area)
    WordCloud(online_wordCloud,
              './code/wordCloudPic/onlineGameAreaWordCloud.png')
    print("网游区词云生成成功")
    anchor = getAnchorData(res, "网游竞技")
    omline_top = computeTopDict(anchor)
    WordCloud(omline_top, './code/wordCloudPic/onlineGameAnchorWordCloud.png')
    print("网游区主播词云生成成功")
    # 单机区爬取
    res = getInterData(urlDict["singleGame"])
    area = getAreaData(res)
    single_wordCloud = computeAreaTotalHotDict(area)
    WordCloud(single_wordCloud,
              './code/wordCloudPic/singlePlayerGameAreaWordCloud.png')
    print("单机区词云生成成功")
    anchor = getAnchorData(res, "单机热游")
    single_top = computeTopDict(anchor)
    WordCloud(single_top,
              './code/wordCloudPic/singlePlayerGameAnchorWordCloud.png')
    print("单机区主播词云生成成功")
    # 全平台词云
    all_wordCloud = Merge(amusement_wordCloud, mobile_wordCloud,
                          online_wordCloud, single_wordCloud)
    print(all_wordCloud)
    WordCloud(all_wordCloud, './code/wordCloudPic/allAreaWordCloud.png')
    print("全平台词云生成成功")
    all_top = Merge(amusement_top, mobile_top, omline_top, single_top)
    WordCloud(all_top, './code/wordCloudPic/allAnchorWordCloud.png')
    print("全平台主播词云生成成功")
